package com.hbnu.collections;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author kittychen
 * @since 2025-10-13 10:19
 */
public class HashSetDemo {

    public static void main(String[] args) {
        Set<String> hashSet = new HashSet<>();

        hashSet.add("element5");
        hashSet.add("element2");
        hashSet.add("element1");
        hashSet.add("element4");
        hashSet.add("element8");
        hashSet.add("element9");
        hashSet.add("element5");
        System.out.println(hashSet);

        System.out.println(hashSet.size());
        System.out.println(hashSet.isEmpty());
        System.out.println(hashSet.contains("element3"));
        hashSet.remove("element5");

        System.out.println("增强for循环遍历set集合元素");
        for (String s : hashSet) {
            System.out.println(s);
        }

        System.out.println("使用集合迭代器遍历set集合元素");
        Iterator<String> iterator = hashSet.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
